package com.xli.mybatis.support;

import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

public class EncryptFactory {

    private static final Map<String, Class<? extends Encrypt>> SERVICE_MAP = new HashMap<>();

    static {
        SERVICE_MAP.put("AES", AesSupport.class);
        SERVICE_MAP.put("SM4", SM4Support.class);
    }

    public static Encrypt getEncryptionService(String type) {
        Class<? extends Encrypt> serviceClass = SERVICE_MAP.get(type);
        if (serviceClass == null) {
            throw new IllegalArgumentException("Unsupported encryption type: " + type);
        }
        return (Encrypt) Proxy.newProxyInstance(serviceClass.getClassLoader(), new Class<?>[]{Encrypt.class}, (proxy, method, args) -> {
            Encrypt instance = serviceClass.getDeclaredConstructor().newInstance();
            return method.invoke(instance, args);
        });
    }
}
